Forum Discussion
How to use Power BI Rest API without GUI authentication (redirect uri)
- 10 years ago
You can use the user name and password flow that AAD supports. This 3rd party client library has an example: https://github.com/Vtek/PowerBI.Api.Client
Below is the code for getting AccessToken by giving User credential, client Id (without AccessCode & Azure-Login UI).
using Newtonsoft.Json;
using System.IO;
using System.Text;
private async void SetAccessToken()
{
List<KeyValuePair<string, string>> vals = new List<KeyValuePair<string, string>>();
vals.Add(new KeyValuePair<string, string>("grant_type", "password"));
vals.Add(new KeyValuePair<string, string>("scope", "openid"));
vals.Add(new KeyValuePair<string, string>("resource", "https://analysis.windows.net/powerbi/api"));
vals.Add(new KeyValuePair<string, string>("client_id", ""));
vals.Add(new KeyValuePair<string, string>("client_secret", ""));
vals.Add(new KeyValuePair<string, string>("username", ""));
vals.Add(new KeyValuePair<string, string>("password", ""));
string TenantId = "";
string url = string.Format("https://login.windows.net/{0}/oauth2/token", TenantId);
HttpClient hc = new HttpClient();
HttpContent content = new FormUrlEncodedContent(vals);
HttpResponseMessage hrm = hc.PostAsync(url, content).Result;
string responseData = "";
if (hrm.IsSuccessStatusCode)
{
Stream data = await hrm.Content.ReadAsStreamAsync();
using (StreamReader reader = new StreamReader(data, Encoding.UTF8))
{
responseData = reader.ReadToEnd();
}
}
Token = JsonConvert.DeserializeObject<AccessToken>(responseData);
}
public class AccessToken
{
public string token_type;
public string scope { get; set; }
public string expires_in { get; set; }
public string expires_on { get; set; }
public string not_before { get; set; }
public string resource { get; set; }
public string access_token { get; set; }
public string refresh_token { get; set; }
public string id_token { get; set; }
}
Hi there,
What do you use for the Username/Password for your solution.
We use SSO at our company so not sure what account I can use.
- heitzmanjared10 years agoFrequent Visitor
I currently use my personal credentials. We're planning a change soon. What sucks is that PowerBI doesn't really allow multiple users to build reports off an API dataset. Our plan is to get a shared Service Account, pay for a license for the service account, and keep the password private to our group.
- Phil_Seamark10 years ago
Microsoft Employee
So in my case, when I log into Power BI using our company SSO, I use the my internal company AD network username and password.
Are you saying these should work ok?
Oh and I take it your code is designed to work with a Web app? I'm trying to use and SSIS script task to push rows up to a Dataset\table.
- heitzmanjared10 years agoFrequent Visitor
The big catch is that you have to give permissions the first time. In my case, the account that "owns" the dataset is my personal account. I had to set up my app to use traditional authentication wiht a redirect, and go through the typical permisson acceptance to let PowerBI know that I approve the permissions I identified when registering the app. Once i did that, I was able to use my username and password and make an automatic call every 30 minutes with no user intervention. If this isn't clear, please let me know and we can have a quick call to clarify